// StandAloneWindow.cpp
//
#include "StandAloneWindow.h"

#include "ViewInterface/View.h"
#include "ViewInterface/ViewEvents.h"
#include "CoordinateManager/UomManager.h"

#include "GlobalResources.h"
#include "GLFrame.h"
#include "GraphicScene2D.h"
#include "UserInteractionManager.h"

#include <QAction>
#include <QActionGroup>
#include <QCloseEvent>
#include <QStatusBar>
#include <QLabel>
#include <QProgressBar>
#include <QMenu>
#include <QIcon>
#include <QPointF>
#include <QAction>
#include <QMessageBox>
#include <QFileDialog>
#include <QGraphicsProxyWidget>
#include <QDockWidget>
#include <QApplication>


#include "MemCheck.h"

using namespace IDS::IDEAS;
using namespace IDS::IDEAS::OpenGLView;
using namespace IDS::IDEAS::OpenGLView::Fwk;

//-----------------------------------------------------------------------------------------
// StandAloneWindow constructor
StandAloneWindow::StandAloneWindow(ViewInterface::ViewGraphic *owner)
: QMainWindow(NULL, Qt::Window),
MainWindow( owner )
{
	glArea					= new GLFrame(this);
	glArea->setScene( new GraphicScene2D(this) );
	setCentralWidget(glArea);

	QApplication::setWindowIcon( QIcon(":/res/Icons/app.png") );
	
	placeHolderMain             = NULL;
	placeHolderPoints           = NULL;
	placeHolderSegments         = NULL;
	placeHolderArcs             = NULL;
	placeHolderLoci		        = NULL;
	placeHolderShapes           = NULL;
	placeHolderCurveOperations  = NULL;
	placeHolderShapeOperations  = NULL;
	placeHolderMeasures         = NULL;

	dockableToolBar				= NULL;

	_init_createActions();
	_init_createMenus();
	_init_createToolBars();
	_init_statusBar();	
	
	borders_w = 0;
	borders_h = 0;

	qApp->setStyle(new MyProxyStyle);	
}

//----------------------------------------------------------------------------------------------
// StandAloneWindow Destructor.
StandAloneWindow::~StandAloneWindow()
{
	deleteLater();
	if(glArea)
	{
		delete glArea;
		glArea = NULL;
	}

	if(uim)
	{
		delete uim;
		uim = NULL;
	}


	GlobalResources::destroy();

	_deinit_destroyActions();
	_deinit_destroyMenus();
	_deinit_destroyToolBars();
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::setWindowTitle( const std::string &title )
{
	QMainWindow::setWindowTitle( QString( title.c_str() ) );
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::_init_statusBar()
{
	statusBar = new QStatusBar(this);

	statusText = new QLabel("Ready");
	statusText->setAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 

	coordXY = new QLabel();
	coordXY->setAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 

	coordLonLat = new QLabel();
	coordLonLat->setAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 
	
	progressBar = new QProgressBar();
	progressBar->setVisible(false);
	progressBar->setTextVisible( true );
	progressBar->setRange( 0, 100 );
	progressBar->setAlignment( Qt::AlignCenter );

	statusBar->insertWidget( 0, statusText, 1 );
	statusBar->insertWidget( 1, coordXY,    1 );
	statusBar->insertWidget( 2, coordLonLat,1 );
	statusBar->insertWidget( 3, progressBar   );
	
	setStatusBar( statusBar );
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::_init_createActions()
{
	GraphicScene *g = dynamic_cast<GraphicScene*>(glArea->scene());

	if( !g ) return;


	openAction = new QAction( "&Open...", this);
	openAction->setShortcut( QKeySequence( "Ctrl+O" ) );
	connect( openAction, SIGNAL(triggered()), this, SLOT(command_openFile()) );

	saveAction = new QAction( "&Save...", this);
	saveAction->setShortcut( QKeySequence( "Ctrl+S" ) );
	connect( saveAction, SIGNAL(triggered()), this, SLOT(command_saveFile()) );

	exitAction = new QAction( "E&xit", this);
	connect( exitAction, SIGNAL(triggered()), this, SLOT(command_exit()) );

	
	// SELECTION -------------------------------------------------------------------------------------
	mainActions.push_back( new QAction(QIcon(":/res/Icons/902.png"), tr("Select / Move"), this) );
	connect(mainActions.back(), SIGNAL(triggered()),g,SLOT(command_translate()));

	mainActions.push_back( new QAction(QIcon(":/res/Icons/901.png"), tr("Select / Rotate"), this) );
	connect(mainActions.back(), SIGNAL(triggered()),g,SLOT(command_rotate()));

	mainActions.push_back( new QAction(QIcon(":/res/Icons/701.png"), tr("Select / Explore"), this) );
	connect(mainActions.back(), SIGNAL(triggered()),g,SLOT(command_explore()));
	// --------------------------------------------------------------------------------------------

	// POINTS -------------------------------------------------------------------------------------
	pointsActions.push_back( new QAction(QIcon(":/res/Icons/005.png"), tr("Create Point At Arc Center"), this));
	connect(pointsActions.back(), SIGNAL(triggered()),g,SLOT(command_createPointAtArcCenter()));

	pointsActions.push_back( new QAction(QIcon(":/res/Icons/004.png"), tr("Project Point on Curve"), this) );
	connect(pointsActions.back(), SIGNAL(triggered()),g,SLOT(command_createPointOnCurve()));

	pointsActions.push_back( new QAction(QIcon(":/res/Icons/007.png"), tr("Create Points At Curves Intersections"), this));
	connect(pointsActions.back(), SIGNAL(triggered()),g,SLOT(command_createPointAtIntersection()));

	pointsActions.push_back( new QAction(QIcon(":/res/Icons/001.png"), tr("Create a Point"), this) );
	connect(pointsActions.back(), SIGNAL(triggered()),g,SLOT(command_createPoint()));
	// --------------------------------------------------------------------------------------------
	
	// SEGMENTS -----------------------------------------------------------------------------------
	//actCreateSegmentFromPointDirectionAndEndPoint = new QAction(QIcon(":/res/Icons/103.png"), tr("Create Segment (Dir && EndPoint)"), this);
	//connect(actCreateSegmentFromPointDirectionAndEndPoint, SIGNAL(triggered()),g,SLOT(command_createSegmentFromPointDirectionAndEndPoint()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/105.png"), tr("Create Tangent to Curve by Points"), this));
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createTangentToCurveGivenEndPoint()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/104.png"), tr("Create Tangent to Curve (Length)"), this));
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createTangentToCurveGivenLength()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/108.png"), tr("Create Tangent to Curve given Angle and Line"), this));
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createTangentToCurveGivenSegmentAndAngle()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/105.png"), tr("Create Tangent to Curve From Point"), this));
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createTangentToCurveFromPoint()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/107.png"), tr("Create Orthogonal Line to a Curve"), this));
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createSegmentPerpendicularToCurve()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/806.png"), tr("Create Angle Bisector"), this));
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createAngleBisector()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/304.png"), tr("Create a Polyline"), this));
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createPolyline()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/102.png"), tr("Create Line (Dir && Length)"), this) );
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createSegmentFromPointDirectionAndLength()));

	segmentsActions.push_back( new QAction(QIcon(":/res/Icons/101.png"), tr("Create Line"), this) );
	connect(segmentsActions.back(), SIGNAL(triggered()),g,SLOT(command_createSegment()));
	// --------------------------------------------------------------------------------------------

	// ARCS ---------------------------------------------------------------------------------------
	arcsActions.push_back( new QAction(QIcon(":/res/Icons/206.png"), tr("Create Tangent Arc to a Curve"), this) );
	connect(arcsActions.back() , SIGNAL(triggered()),g,SLOT(command_createArcTangentToCurveAtPointAndEndPoint()));

	arcsActions.push_back( new QAction(QIcon(":/res/Icons/205.png"), tr("Create Tangent Arc by radius"), this));
	connect(arcsActions.back(), SIGNAL(triggered()),g,SLOT(command_createArcTangentToCurveAtPointGivenRadiusAndEndPoint()));

	arcsActions.push_back( new QAction(QIcon(":/res/Icons/208.png"), tr("Create an Arc joining two Lines"), this));
	connect(arcsActions.back(), SIGNAL(triggered()),g,SLOT(command_createCircularJoint()));
	
	arcsActions.push_back( new QAction(QIcon(":/res/Icons/204.png"), tr("Create Arc by Center and Two Points"), this));
	connect(arcsActions.back(), SIGNAL(triggered()),g,SLOT(command_createArcFromCenterAndTwoPoints()));

	arcsActions.push_back( new QAction(QIcon(":/res/Icons/203.png"), tr("Create Arc by Center and Sweep Angle"), this));
	connect(arcsActions.back(), SIGNAL(triggered()),g,SLOT(command_createArcFromCenterAndPointGivenAngle()));

	arcsActions.push_back( new QAction(QIcon(":/res/Icons/202.png"), tr("Create Arc by Center and Arc Length"), this));
	connect(arcsActions.back(), SIGNAL(triggered()),g,SLOT(command_createArcFromCenterAndPointGivenLength()));

	arcsActions.push_back( new QAction(QIcon(":/res/Icons/201.png"), tr("Create Arc by Three Points"), this));
	connect(arcsActions.back(), SIGNAL(triggered()),g,SLOT(command_createArcFrom3Points()));
	// --------------------------------------------------------------------------------------------

	// LOCUS --------------------------------------------------------------------------------------
	lociActions.push_back( new QAction(QIcon(":/res/Icons/805.png"), tr("Create a ArcLocus"), this) );
	connect(lociActions.back(), SIGNAL(triggered()),g,SLOT(command_createArcLocus()));

	lociActions.push_back( new QAction(QIcon(":/res/Icons/804.png"), tr("Create a SegLocus"), this) );
	connect(lociActions.back(), SIGNAL(triggered()),g,SLOT(command_createSegLocus()));

	lociActions.push_back( new QAction(QIcon(":/res/Icons/801.png"), tr("Create Windspiral"), this) );
	connect(lociActions.back(), SIGNAL(triggered()),g,SLOT(command_createWindspiral()));
	// --------------------------------------------------------------------------------------------

	// MEASURES -----------------------------------------------------------------------------------
	measuresActions.push_back( new QAction(QIcon(":/res/Icons/604.png"), tr("Measure Angle by Three Points"), this) );
	connect(measuresActions.back(), SIGNAL(triggered()),g,SLOT(command_measureAngleByThreePoints()));

	measuresActions.push_back( new QAction(QIcon(":/res/Icons/603.png"), tr("Measure Angles at Curves Intersections"), this) );
	connect(measuresActions.back(), SIGNAL(triggered()),g,SLOT(command_measureAngleAtCurveIntersections()));

	measuresActions.push_back( new QAction(QIcon(":/res/Icons/802.png"), tr("Measures"), this) );
	connect(measuresActions.back(), SIGNAL(triggered()),g,SLOT(command_measures()));
	// --------------------------------------------------------------------------------------------
	
	// SHAPES -------------------------------------------------------------------------------------
	shapesActions.push_back( new QAction(QIcon(":/res/Icons/405.png"), tr("Create a Shape from Curves"), this) );
	connect(shapesActions.back(), SIGNAL(triggered()),g,SLOT(command_createShapeFromCurves()));

	shapesActions.push_back( new QAction(QIcon(":/res/Icons/403.png"), tr("Create a Rectangle from Two Points"), this) );
	connect(shapesActions.back(), SIGNAL(triggered()),g,SLOT(command_createRectangleFromTwoPoints()));

	shapesActions.push_back( new QAction(QIcon(":/res/Icons/402.png"), tr("Create a Circle from Two Points"), this) );
	connect(shapesActions.back(), SIGNAL(triggered()),g,SLOT(command_createCircleFromTwoPoints()));

	//shapesActions.push_back( new QAction(QIcon(":/res/Icons/401.png"), tr("Create a Circle from Center and Radius"), this) );
	//connect(shapesActions.back(), SIGNAL(triggered()),g,SLOT(command_createCircleFromCenterAndRadius()));

	shapesActions.push_back( new QAction(QIcon(":/res/Icons/404.png"), tr("Create a Polygon from Points"), this) );
	connect(shapesActions.back(), SIGNAL(triggered()),g,SLOT(command_createPolygonFromPoints()));
	// --------------------------------------------------------------------------------------------

	// SHAPES OPERATIONS --------------------------------------------------------------------------
	shapeOperationsActions.push_back( new QAction(QIcon(":/res/Icons/501.png"), tr("Union of Two Shapes"), this) );
	connect(shapeOperationsActions.back(), SIGNAL(triggered()),g,SLOT(command_unionOfTwoShapes()));

	shapeOperationsActions.push_back( new QAction(QIcon(":/res/Icons/502.png"), tr("Intersection of Two Shapes"), this) );
	connect(shapeOperationsActions.back(), SIGNAL(triggered()),g,SLOT(command_intersectionOfTwoShapes()));

	shapeOperationsActions.push_back( new QAction(QIcon(":/res/Icons/503.png"), tr("Difference of Two Shapes"), this) );
	connect(shapeOperationsActions.back(), SIGNAL(triggered()),g,SLOT(command_differenceOfTwoShapes()));
	// --------------------------------------------------------------------------------------------

	// MISC ---------------------------------------------------------------------------------------
	operationsActions.push_back( new QAction(QIcon(":/res/Icons/302.png"), tr("Split Curve at Desired Point"), this));
	connect(operationsActions.back(), SIGNAL(triggered()),g,SLOT(command_splitCurveAtPoint()));

	operationsActions.push_back( new QAction(QIcon(":/res/Icons/803.png"), tr("Bufferize"), this));
	connect(operationsActions.back(), SIGNAL(triggered()),g,SLOT(command_bufferize()));
	// --------------------------------------------------------------------------------------------

	actCloneObj		= new QAction(QIcon(":/res/Icons/702.png"), tr("Clone Objects"), this);
	connect(actCloneObj, SIGNAL(triggered()),g,SLOT(command_cloneObject()));

	actDeleteObj	= new QAction(QIcon(":/res/Icons/705.png"), tr("Delete Object"), this);
	connect(actDeleteObj, SIGNAL(triggered()),g,SLOT(command_deleteSelectedObject()));

	actFitView		= new QAction(QIcon(":/res/Icons/703.png"), tr("Fit View"), this);
	connect(actFitView, SIGNAL(triggered()),g,SLOT(command_fitView()));

	actEndCapture = new QAction("End",this);
	connect(actEndCapture, SIGNAL(triggered()),g,SLOT(command_EndCaptureModify()));

	// --------------------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------------------
	placeHolderMain = new QAction(this); connect(placeHolderMain, SIGNAL(triggered()),SLOT(fireMainAction()));
	placeHolderMain->setCheckable(true);
	placeHolderMain->setChecked(true);

	placeHolderPoints = new QAction(this); connect(placeHolderPoints, SIGNAL(triggered()),SLOT(firePointsAction()));
	placeHolderPoints->setCheckable(true);

	placeHolderSegments = new QAction(this); connect(placeHolderSegments, SIGNAL(triggered()),SLOT(fireSegmentsAction()));
	placeHolderSegments->setCheckable(true);

	placeHolderArcs = new QAction(this);	connect(placeHolderArcs, SIGNAL(triggered()),SLOT(fireArcsAction()));
	placeHolderArcs->setCheckable(true);

	//placeHolderTangents = new QAction(this);	connect(placeHolderTangents, SIGNAL(triggered()),SLOT(fireTangentsAction()));
	//placeHolderTangents->setCheckable(true);

	//placeHolderWindspiral = new QAction(this);	connect(placeHolderWindspiral, SIGNAL(triggered()),SLOT(fireWindspiralAction()));
	//placeHolderWindspiral->setCheckable(true);

	placeHolderLoci = new QAction(this);	connect(placeHolderLoci, SIGNAL(triggered()),SLOT(fireLociAction()));
	placeHolderLoci->setCheckable(true);

	placeHolderShapes = new QAction(this);	connect(placeHolderShapes, SIGNAL(triggered()),SLOT(fireShapesAction()));
	placeHolderShapes->setCheckable(true);

	placeHolderCurveOperations = new QAction(this);	connect(placeHolderCurveOperations, SIGNAL(triggered()),SLOT(fireCurveOperationsAction()));
	placeHolderCurveOperations->setCheckable(true);

	placeHolderShapeOperations = new QAction(this);	connect(placeHolderShapeOperations, SIGNAL(triggered()),SLOT(fireShapeOperationsAction()));
	placeHolderShapeOperations->setCheckable(true);

	placeHolderMeasures = new QAction(this);	connect(placeHolderMeasures, SIGNAL(triggered()),SLOT(fireMeasuresAction()));
	placeHolderMeasures->setCheckable(true);

	loadShortcuts();
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::loadShortcuts()
{
	GraphicScene *g = dynamic_cast<GraphicScene*>(glArea->scene());
	if( !g )
		return;

	uim			 = new UserInteractionManager;

	// TODO: LOAD FROM FILE
	uim->addKeyBinding("FitView",		"Fit View",		     "Space",  g, SLOT(command_fitView()) );
	uim->addKeyBinding("ShowGrid",   	"Show Grid",	     "Ctrl+g", g, SLOT(command_toggleGrid()) );
	uim->addKeyBinding("ShowBorders",	"Show Borders",      "Ctrl+b", g, SLOT(command_toggleBorders()) );
	uim->addKeyBinding("Copy",			"Copy",			     "Ctrl+c", g, SLOT(command_cloneObject()) );
	uim->addKeyBinding("Paste",			"Paste",		     "Ctrl+v", g, SLOT(command_pasteObject()) );
	uim->addKeyBinding("CreateNewLayer","",				     "Ctrl+a", g, SLOT(command_addLayer()) );
	uim->addKeyBinding("SunShading",	    "Toggle Sun Shading","Ctrl+l", g, SLOT(command_toggleSunShading()) );
	//uim->addKeyBinding("LoadCurves",	"Load Curves",	     "Ctrl+F5",g, SLOT(command_loadRedLines()) );
	//uim->addKeyBinding("SaveCurves",	"Save Curves",	     "Ctrl+F6",g, SLOT(command_saveRedLines()) );
#ifdef _DEBUG	
	uim->addKeyBinding("DebugInfo",		"Show Info",	"Ctrl+i", g, SLOT(debug_showDBGInfo()) );
#endif
	uim->addKeyBinding("DeleteObject",	"Delete",		"Delete", g, SLOT(command_deleteSelectedObject()) );
	uim->addKeyBinding("Plus",			"",				"Plus",	  g, SLOT(debug_plusCommand()) );
	uim->addKeyBinding("Minus",			"",				"Minus",  g, SLOT(debug_minusCommand()) );
	uim->addKeyBinding("Reset",			"",				"Esc",    g, SLOT(resetCurrentAction()) );

	uim->addKeyBinding("TB_Shortcut_1",	"", "F1", this, SLOT(cycleMainAction()));
	uim->addKeyBinding("TB_Shortcut_2",	"", "F2", this, SLOT(cyclePointsAction()));
	uim->addKeyBinding("TB_Shortcut_3",	"", "F3", this, SLOT(cycleSegmentsAction()));
	uim->addKeyBinding("TB_Shortcut_4",	"", "F4", this, SLOT(cycleArcsAction()));
	uim->addKeyBinding("TB_Shortcut_5",	"", "F5", this, SLOT(cycleLociAction()));
	uim->addKeyBinding("TB_Shortcut_6",	"", "F6", this, SLOT(cycleShapesAction()));
	uim->addKeyBinding("TB_Shortcut_7",	"", "F7", this, SLOT(cycleShapeOperationsAction()));
	uim->addKeyBinding("TB_Shortcut_8",	"", "F8", this, SLOT(cycleCurveOperationsAction()));
	uim->addKeyBinding("TB_Shortcut_9",	"", "F9", this, SLOT(cycleMeasuresAction()));

	uim->addKeyBinding("Up",    "",	"Ctrl+UpArrow",    g, SLOT(command_moveSunUp()) );
	uim->addKeyBinding("Down",	"",	"Ctrl+DownArrow",  g, SLOT(command_moveSunDown()) );
	uim->addKeyBinding("Left",	"",	"Ctrl+LeftArrow",  g, SLOT(command_moveSunLeft()) );
	uim->addKeyBinding("Right",	"",	"Ctrl+RightArrow", g, SLOT(command_moveSunRight()) );

#ifdef TEST_APP	
	uim->addKeyBinding("Reset",	"",	"Q", g, SLOT(resetCurrentAction()) );
	uim->addKeyBinding("Up",	    "",	"W", g, SLOT(command_moveSunUp()) );
	uim->addKeyBinding("Down",	"",	"S", g, SLOT(command_moveSunDown()) );
	uim->addKeyBinding("Left",	"",	"A", g, SLOT(command_moveSunLeft()) );
	uim->addKeyBinding("Right",	"",	"D", g, SLOT(command_moveSunRight()) );
#endif

	uim->addMouseBinding("Zoom","Wheel");
	addActions( uim->getKeyBindings() );

	g->setUIM(uim);
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::command_openFile()
{
	GraphicScene *g = dynamic_cast<GraphicScene*>(glArea->scene());

	if( !g )
		return;


	QString fileName = QFileDialog::getOpenFileName(
						this,
						tr("Open"),
						QDir::currentPath(),
						tr("All supported formats (*.txt *.kml);;IDS Geometry Files (*.txt);;KML Files (*.kml)"));
	
	if( fileName.isEmpty() )
		return;


	int mode = -1;
	if( fileName.endsWith(".kml", Qt::CaseInsensitive) )		mode = 1;
	else if(fileName.endsWith(".txt", Qt::CaseInsensitive) )	mode = 0;
	else														mode = -1;

	if( mode == -1 )
	{
		QMessageBox::critical(this, "Loading error", "File format not supported!", QMessageBox::Ok, QMessageBox::NoButton);
		return;
	}

	if( g->loadGeometry( fileName, mode) )
		printf("Loading %s: successful!\n",fileName.toLocal8Bit().data() );
	else
	{
		QMessageBox::critical(this, "Loading error", "Failed to load "+fileName+"!", QMessageBox::Ok, QMessageBox::NoButton);
		printf("Loading %s: failed\n",fileName.toLocal8Bit().data() );
	}
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::command_saveFile()
{
	GraphicScene *g = dynamic_cast<GraphicScene*>(glArea->scene());

	if( !g )
		return;

#define KML_EXPORT_DISABLED 1

	QString fileName = QFileDialog::getSaveFileName(
						this,
						tr("Save"),
						QDir::currentPath(),
#if KML_EXPORT_DISABLED
						tr("All supported formats (*.txt *.kml);;IDS Geometry Files (*.txt);;KML Files (*.kml)"));
#else
						tr("All supported formats (*.txt);;IDS Geometry Files (*.txt);;"));
#endif
	
	if( fileName.isEmpty() )
		return;
	
	int mode = -1;
	if( fileName.endsWith(".kml", Qt::CaseInsensitive) )		
#if KML_EXPORT_DISABLED
		mode = 1; 
#else
		mode = -1;
#endif
	else if(fileName.endsWith(".txt", Qt::CaseInsensitive) )	mode = 0;
	else														mode = -1;

	if( mode == -1 )
	{
		QMessageBox::critical(this, "Saving error", "File format not supported!", QMessageBox::Ok, QMessageBox::NoButton);
		return;
	}

	if( g->saveGeometry( fileName, mode, 1) )
		printf("Saving %s: successful!\n",fileName.toLocal8Bit().data() );
	else
		printf("Saving %s: failed\n",fileName.toLocal8Bit().data() );
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::command_exit()
{
	close();

	if( view )
		view->sendEvent( new ViewInterface::CloseViewEvent() );
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::showWindow()
{
	show();
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::_init_createMenus()
{
	QMenu *fileMenu = menuBar()->addMenu(QString("&File"));
	fileMenu->addAction( openAction );
	fileMenu->addAction( saveAction );
	fileMenu->addSeparator();
	fileMenu->addAction( exitAction );

	//QMenu *saveMenu = fileMenu->addMenu();
	//saveMenu->addAction(uim->getAction("LoadCurves"));
	//saveMenu->addAction(uim->getAction("SaveCurves"));
	//fileMenu->addSeparator();
	QAction *a;
	a = uim->getAction("ShowGrid");    a->setCheckable(true); a->setChecked(true);
	a = uim->getAction("ShowBorders"); a->setCheckable(true); a->setChecked(true);
	
	QMenu *viewMenu = menuBar()->addMenu(QString("&View"));
	viewMenu->addAction( uim->getAction("FitView") );
	viewMenu->addAction( uim->getAction("ShowGrid") );
	viewMenu->addAction( uim->getAction("ShowBorders") );
#if 0
	viewMenu->addAction( uim->getAction("DebugInfo") );
	viewMenu->addAction( uim->getAction("SunShading") );

	QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
	editMenu->addAction( uim->getAction("Copy") );
	editMenu->addAction( uim->getAction("Paste") );
	editMenu->addAction( uim->getAction("Delete") );
	editMenu->addSeparator();

	for(int i=mainActions.size()-1; i>=0; --i)
		editMenu->addAction( mainActions[i] );

	QMenu *m;
	m->setI
	editMenu->addSeparator();
	m = editMenu->addMenu("Create Point");
	for(int i=pointsActions.size()-1; i>=0; --i)
		m->addAction( pointsActions[i] );
	editMenu->addSeparator();

	menuBar()->addSeparator();
#endif
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::_init_createToolBars()
{
	GraphicScene *g = dynamic_cast<GraphicScene*>(glArea->scene());
	if( !g )
		return;

	toolBar = new QToolBar();

	// MAIN -------------------------------------------------------------------------
	toolBar->addAction( placeHolderMain );
	QMenu *menuMain = new QMenu(this);
	connect(menuMain, SIGNAL(triggered(QAction*)),SLOT(swapMainAction(QAction*)));
	placeHolderMain->setMenu(menuMain);

	for(size_t i=0; i<mainActions.size(); ++i)
		menuMain->addAction( mainActions[i] );

	swapMainAction(mainActions.back(),false);	
	toolBar->addSeparator();
	// ------------------------------------------------------------------------------

	// POINTS -----------------------------------------------------------------------
	toolBar->addAction( placeHolderPoints );	
	QMenu *menuPoints = new QMenu(this);
	connect(menuPoints, SIGNAL(triggered(QAction*)),SLOT(swapPointsAction(QAction*)));
	placeHolderPoints->setMenu(menuPoints);

	for(size_t i=0; i<pointsActions.size(); ++i)
		menuPoints->addAction( pointsActions[i] );
	
	swapPointsAction(pointsActions.back(),false);
	// ------------------------------------------------------------------------------

	// SEGMENTS ---------------------------------------------------------------------
	toolBar->addAction( placeHolderSegments );
	QMenu *menuSegments = new QMenu(this);
	connect(menuSegments, SIGNAL(triggered(QAction*)),SLOT(swapSegmentsAction(QAction*)));
	placeHolderSegments->setMenu(menuSegments);

	for(size_t i=0; i<segmentsActions.size(); ++i)
		menuSegments->addAction( segmentsActions[i] );

	swapSegmentsAction(segmentsActions.back(),false);	
	// ------------------------------------------------------------------------------

	// ARCS -------------------------------------------------------------------------
	toolBar->addAction( placeHolderArcs );	
	QMenu *menuArcs = new QMenu(this);
	connect(menuArcs, SIGNAL(triggered(QAction*)),SLOT(swapArcsAction(QAction*)));
	placeHolderArcs->setMenu(menuArcs);

	for(size_t i=0; i<arcsActions.size(); ++i)
		menuArcs->addAction( arcsActions[i] );

	swapArcsAction(arcsActions.back(),false);	
	// ------------------------------------------------------------------------------

	// LOCUS ------------------------------------------------------------------------
	toolBar->addAction( placeHolderLoci );
	QMenu *menuLoci = new QMenu(this);
	connect(menuLoci, SIGNAL(triggered(QAction*)),SLOT(swapLociAction(QAction*)));
	placeHolderLoci->setMenu(menuLoci);

	for(size_t i=0; i<lociActions.size(); ++i)
		menuLoci->addAction( lociActions[i] );

	swapLociAction(lociActions.back(),false);
	// ------------------------------------------------------------------------------

	toolBar->addSeparator();

	// SHAPES -----------------------------------------------------------------------
	toolBar->addAction( placeHolderShapes );

	QMenu *menuShapes = new QMenu(this);
	connect(menuShapes, SIGNAL(triggered(QAction*)),SLOT(swapShapesAction(QAction*)));
	placeHolderShapes->setMenu(menuShapes);

	for(size_t i=0; i<shapesActions.size(); ++i)
		menuShapes->addAction( shapesActions[i] );

	swapShapesAction(shapesActions.back(),false);	
	// ------------------------------------------------------------------------------

	// SHAPE OPERATIONS -------------------------------------------------------------
	toolBar->addAction( placeHolderShapeOperations );

	QMenu *menuShapeOperations = new QMenu(this);
	connect(menuShapeOperations, SIGNAL(triggered(QAction*)),SLOT(swapShapeOperationsAction(QAction*)));
	placeHolderShapeOperations->setMenu(menuShapeOperations);

	for(size_t i=0; i<shapeOperationsActions.size(); ++i)
		menuShapeOperations->addAction( shapeOperationsActions[i] );
	
	swapShapeOperationsAction(shapeOperationsActions.back(),false);	
	// ------------------------------------------------------------------------------

	// CURVE OPERATIONS -------------------------------------------------------------
	toolBar->addAction( placeHolderCurveOperations );
	
	QMenu *menuCurveOperations = new QMenu(this);
	connect(menuCurveOperations, SIGNAL(triggered(QAction*)),SLOT(swapCurveOperationsAction(QAction*)));
	placeHolderCurveOperations->setMenu(menuCurveOperations);
	
	for(size_t i=0; i<operationsActions.size(); ++i)
		menuCurveOperations->addAction( operationsActions[i] );	
	
	swapCurveOperationsAction(operationsActions.back(),false);	
	// ------------------------------------------------------------------------------

	toolBar->addSeparator();

	//MEASURES ----------------------------------------------------------------------
	toolBar->addAction( placeHolderMeasures );

	QMenu *menuMeasure = new QMenu(this);
	connect(menuMeasure, SIGNAL(triggered(QAction*)),SLOT(swapMeasuresAction(QAction*)));
	placeHolderMeasures->setMenu( menuMeasure );

	for(size_t i=0; i<measuresActions.size(); ++i)
		menuMeasure->addAction( measuresActions[i] );		
	
	swapMeasuresAction( measuresActions.back(),false);	
	// ------------------------------------------------------------------------------

	// The action group is needed to set only one action active at time
	QActionGroup *ag = new QActionGroup(this);
	ag->addAction(placeHolderMain);
	ag->addAction(placeHolderPoints);
	ag->addAction(placeHolderSegments);
	ag->addAction(placeHolderArcs);
	ag->addAction(placeHolderLoci);
	ag->addAction(placeHolderShapes);
	ag->addAction(placeHolderCurveOperations);
	ag->addAction(placeHolderShapeOperations);
	ag->addAction(placeHolderMeasures);

	// The toolbar is now set!
	// Add it at the bottom of the screen
	addToolBar( Qt::BottomToolBarArea,toolBar );


	// Floating toolbar
	floatingToolBar = new QToolBar();
	floatingToolBar->addAction( actEndCapture );
	floatingToolBar->setVisible(false);

	addToolBar( Qt::TopToolBarArea,floatingToolBar );

	// clipToolbar
	clipToolBar = new QToolBar();
	clipToolBar->setVisible(false);

	actClipSave  = new QAction(QIcon(":/res/Icons/check.png"),     tr("Accept"),this);
	connect(actClipSave, SIGNAL(triggered()),g,SLOT(command_saveClipRegions()));

	actClipDone  = new QAction(QIcon(":/res/Icons/cross.png"),   tr("Cancel"),this);
	connect(actClipDone, SIGNAL(triggered()),g,SLOT(command_cancelClipMode()));

	actClipClean = new QAction(QIcon(":/res/Icons/edit-clear.png"), tr("Clean"),this);
	connect(actClipClean, SIGNAL(triggered()),g,SLOT(command_cleanClipRegions()));

	clipToolBar->addAction( actClipSave );
	clipToolBar->addAction( actClipDone );
	clipToolBar->addSeparator();
	clipToolBar->addAction( actClipClean );
	addToolBar( Qt::TopToolBarArea,clipToolBar );
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::_deinit_destroyActions()
{
	for(size_t i=0; i<mainActions.size(); ++i)
		delete mainActions[i];

	for(size_t i=0; i<pointsActions.size(); ++i)
		delete pointsActions[i];

	for(size_t i=0; i<segmentsActions.size(); ++i)
		delete segmentsActions[i];

	for(size_t i=0; i<arcsActions.size(); ++i)
		delete arcsActions[i];

	for(size_t i=0; i<lociActions.size(); ++i)
		delete lociActions[i];

	for(size_t i=0; i<shapesActions.size(); ++i)
		delete shapesActions[i];

	for(size_t i=0; i<measuresActions.size(); ++i)
		delete measuresActions[i];

	for(size_t i=0; i<operationsActions.size(); ++i)
		delete operationsActions[i];


	delete actCloneObj;
	delete actFitView;
	delete actDeleteObj;
	delete actEndCapture;

	delete openAction;
	delete saveAction;
	delete exitAction;
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::_deinit_destroyMenus()
{
	//delete appMenu;
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::_deinit_destroyToolBars()
{
	delete dockableToolBar;
	delete toolBar;
	delete floatingToolBar;
	delete clipToolBar;
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::setWindowSize(int x, int y, int w, int h)
{
	move( x, y );
	resize( w, h);
	
	//resize frame (workaround to get rid of window's decoration)
	QRect targetRect(x,y,w,h);
	
	QRect r   = frameGeometry();	// r contains decorated sizes
	borders_w = r.width()-targetRect.width();
	borders_h = r.height()-targetRect.height();			
	
	resize( w-borders_w, h-borders_h);
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::closeEvent(QCloseEvent *event)
{
	if( view )
		view->sendEvent( new ViewInterface::CloseViewEvent() );

	if(glArea)
	{
		delete glArea;
		glArea = NULL;
	}

	if(toolBar)
	{
		delete toolBar;
		toolBar = NULL;
	}

	QMainWindow::closeEvent(event);
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::resizeEvent(QResizeEvent *event)
{
	QMainWindow::resizeEvent(event);

	windowWidth  = event->size().width();
	windowHeight = event->size().height();

	if( view )
	{
		IDSBox p;
		p.minCoord[0] = x();
		p.minCoord[1] = y();
		p.minCoord[2] = 0;
		p.maxCoord[0] = x()+windowWidth+borders_w;
		p.maxCoord[1] = y()+windowHeight+borders_h;
		p.maxCoord[2] = 0;

		((ViewInterface::ViewOpenGLQT*)view)->updateWindowPosition(p);
	}	
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::moveEvent(QMoveEvent *event)
{
	QMainWindow::moveEvent(event);

	if( view )
	{
		IDSBox p;
		p.minCoord[0] = x();
		p.minCoord[1] = y();
		p.minCoord[2] = 0;
		p.maxCoord[0] = x()+windowWidth+borders_w;
		p.maxCoord[1] = y()+windowHeight+borders_h;
		p.maxCoord[2] = 0;

		((ViewInterface::ViewOpenGLQT*)view)->updateWindowPosition(p);
	}
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::setStatusMessage(const std::string &status)
{
	statusText->setText( tr(status.c_str()) );
}

//----------------------------------------------------------------------------------------------
MainWindow::ClientSize StandAloneWindow::getClientSize()
{
	ClientSize cs;
	
	if( !glArea )
	{
		cs.width  = 0;
		cs.height = 0;
	}
	else
	{
		QSize qs  = glArea->getClientSize();
		cs.width  = qs.width();
		cs.height = qs.height();
	}

	return cs;
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::setCursor(unsigned int cursorID)
{
	if( glArea )
		glArea->setCursor( (Qt::CursorShape) cursorID );
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::setStatusPosition(const IDSReal2D &chartPoint, const IDSReal2D &geoPoint, double elevation)
{
	coordXY->setText( QString("[%1 | %2]").arg(chartPoint[0],0,'f',3).arg(chartPoint[1],0,'f',3) );

	QString t("");
	try
	{
		t = QString("[%1]     %2     %3")
			.arg( tr(CoordinateManager::getInstance().getGeoDatum().c_str()) )
			.arg( tr(CoordinateManager::getInstance().formatLatitudeToStdString(geoPoint[1]).c_str()) )
			.arg( tr(CoordinateManager::getInstance().formatLongitudeToStdString(geoPoint[0]).c_str()) );

		if( isfinite(elevation) )
		{
			double h = UomManager::getInstance().toWorkingUom(elevation, UomDistVert::VERM );

			QString s = QString("  |  Elev [%1]: %2 %3")
				.arg( tr(CoordinateManager::getInstance().getVerticalDatum().c_str()) )
				.arg( h,0,'f',3 )
				.arg( tr(UomManager::getInstance().getWorkingUomDistVertAsString().c_str()).toLower() );
			t += s;
		}
	}
	catch(...) {t = QString(""); }

	coordLonLat->setText( t );
	//printf("%s\n",coordXY->text().toLocal8Bit().data());
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::setStatusPercentage(double percent)
{
	const int range = 100;

	if(percent == 0.0 && !showProgressBar)
	{
		showProgressBar = true;
		progressBar->setVisible(true);
	}

	if(percent >= 1.0)
	{
		showProgressBar = false;
		progressBar->setVisible(false);
	}

	progressBar->setValue((int)(percent*range));
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::updateToolbar()
{
}

//----------------------------------------------------------------------------------------------
void StandAloneWindow::showToolbars()
{
	ViewInterface::ViewGraphic::Status s = getSceneStatus();
	
	toolBar->setVisible(false);
	floatingToolBar->setVisible(false);
	clipToolBar->setVisible(false);

	if(s == ViewInterface::ViewGraphic::NORMAL || s == ViewInterface::ViewGraphic::CLIP_IMAGE)
	{
		toolBar->setVisible(true);
	}	
	
	if( s != ViewInterface::ViewGraphic::NORMAL && s != ViewInterface::ViewGraphic::CLIP_IMAGE )
	{
		floatingToolBar->setVisible(true);
	}

	if( s == ViewInterface::ViewGraphic::CLIP_IMAGE )
	{
		clipToolBar->setVisible(true);
	}
}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::dockWindow(void *window, void *action, DockType dockArea)
{
	Qt::DockWidgetArea d;
	switch(dockArea)
	{
	case MainWindow::DOCKBOTTOM: d = Qt::BottomDockWidgetArea;	break;
	case MainWindow::DOCKTOP:	 d = Qt::TopDockWidgetArea;		break;
	case MainWindow::DOCKRIGHT:	 d = Qt::RightDockWidgetArea;	break;
	case MainWindow::DOCKLEFT:	 d = Qt::LeftDockWidgetArea;	break;
	}

	QDockWidget *dw = (QDockWidget *)window;
	QAction *qaction = (QAction *)action;

	if( action )
	{
		if( !dockableToolBar )
		{
			dockableToolBar = new QToolBar(this);
			dockableToolBar->setFloatable( true );
			dockableToolBar->setMovable( true );
		}
		
		dockableToolBar->addAction( qaction );
		addToolBar( Qt::BottomToolBarArea, dockableToolBar );
	}
	
	dw->setVisible( qaction == NULL );
	addDockWidget( d, dw);
}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::fireMainAction()	{placeHolderMain->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::firePointsAction(){placeHolderPoints->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::fireSegmentsAction(){placeHolderSegments->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::fireArcsAction(){placeHolderArcs->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
//void StandAloneWindow::fireTangentsAction(){placeHolderTangents->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
//void StandAloneWindow::fireWindspiralAction(){placeHolderWindspiral->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::fireLociAction(){placeHolderLoci->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::fireShapesAction(){placeHolderShapes->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::fireCurveOperationsAction(){placeHolderCurveOperations->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::fireShapeOperationsAction(){placeHolderShapeOperations->data().value<QAction*>()->trigger();}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::fireMeasuresAction(){placeHolderMeasures->data().value<QAction*>()->trigger();}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapMainAction(QAction *src, bool fire)
{
	placeHolderMain->setIcon( src->icon() );
	placeHolderMain->setText( src->text() );
	placeHolderMain->setToolTip( src->toolTip() );
	placeHolderMain->setData( QVariant::fromValue<QAction*>(src) );
		
	if(fire)
	{
		placeHolderMain->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapPointsAction(QAction *src, bool fire)
{
	placeHolderPoints->setIcon( src->icon() );
	placeHolderPoints->setText( src->text() );
	placeHolderPoints->setToolTip( src->toolTip() );
	placeHolderPoints->setData( QVariant::fromValue<QAction*>(src) );
		
	if(fire)
	{
		placeHolderPoints->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapSegmentsAction(QAction *src, bool fire)
{
	placeHolderSegments->setIcon( src->icon() );
	placeHolderSegments->setText( src->text() );
	placeHolderSegments->setToolTip( src->toolTip() );
	placeHolderSegments->setData( QVariant::fromValue<QAction*>(src) );
		
	if(fire)
	{
		placeHolderSegments->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapArcsAction(QAction *src, bool fire)
{
	placeHolderArcs->setIcon( src->icon() );
	placeHolderArcs->setText( src->text() );
	placeHolderArcs->setToolTip( src->toolTip() );
	placeHolderArcs->setData( QVariant::fromValue<QAction*>(src) );
		
	if(fire)
	{
		placeHolderArcs->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}
//-------------------------------------------------------------------------------------------------
//void StandAloneWindow::swapTangentsAction(QAction *src, bool fire)
//{
//	placeHolderTangents->setIcon( src->icon() );
//	placeHolderTangents->setText( src->text() );
//	placeHolderTangents->setToolTip( src->toolTip() );
//	placeHolderTangents->setData( QVariant::fromValue<QAction*>(src) );
//		
//	if(fire)
//	{
//		placeHolderTangents->setChecked(true);
//		src->blockSignals(true);
//		src->trigger();
//		src->blockSignals(false);
//	}
//}
//-------------------------------------------------------------------------------------------------
//void StandAloneWindow::swapWindspiralAction(QAction *src, bool fire)
//{
//	placeHolderWindspiral->setIcon( src->icon() );
//	placeHolderWindspiral->setText( src->text() );
//	placeHolderWindspiral->setToolTip( src->toolTip() );
//	placeHolderWindspiral->setData( QVariant::fromValue<QAction*>(src) );
//		
//	if(fire)
//	{
//		placeHolderWindspiral->setChecked(true);
//		src->blockSignals(true);
//		src->trigger();
//		src->blockSignals(false);
//	}
//}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapLociAction(QAction *src, bool fire)
{
	placeHolderLoci->setIcon( src->icon() );
	placeHolderLoci->setText( src->text() );
	placeHolderLoci->setToolTip( src->toolTip() );
	placeHolderLoci->setData( QVariant::fromValue<QAction*>(src) );
		
	if(fire)
	{
		placeHolderLoci->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapShapesAction(QAction *src, bool fire)
{
	placeHolderShapes->setIcon( src->icon() );
	placeHolderShapes->setText( src->text() );
	placeHolderShapes->setToolTip( src->toolTip() );
	placeHolderShapes->setData( QVariant::fromValue<QAction*>(src) );
		
	if(fire)
	{
		placeHolderShapes->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapCurveOperationsAction(QAction *src, bool fire)
{
	placeHolderCurveOperations->setIcon( src->icon() );
	placeHolderCurveOperations->setText( src->text() );
	placeHolderCurveOperations->setToolTip( src->toolTip() );
	placeHolderCurveOperations->setData( QVariant::fromValue<QAction*>(src) );
		
	if(fire)
	{
		placeHolderCurveOperations->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapShapeOperationsAction(QAction *src, bool fire)
{
	placeHolderShapeOperations->setIcon( src->icon() );
	placeHolderShapeOperations->setText( src->text() );
	placeHolderShapeOperations->setToolTip( src->toolTip() );
	placeHolderShapeOperations->setData( QVariant::fromValue<QAction*>(src) );
	
	if(fire)
	{
		placeHolderShapeOperations->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::swapMeasuresAction(QAction *src, bool fire)
{
	placeHolderMeasures->setIcon( src->icon() );
	placeHolderMeasures->setText( src->text() );
	placeHolderMeasures->setToolTip( src->toolTip() );
	placeHolderMeasures->setData( QVariant::fromValue<QAction*>(src) );
	
	if(fire)
	{
		placeHolderMeasures->setChecked(true);
		src->blockSignals(true);
		src->trigger();
		src->blockSignals(false);
	}
}



//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cycleMainAction()
{
	static size_t idx = mainActions.size()-1;

	// cycle or switch action ?
	if( placeHolderMain->isChecked() )
	{
		idx++;
		if( idx > mainActions.size()-1 ) idx = 0;	
	}

	swapMainAction(mainActions[idx],false);
	fireMainAction();
}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cyclePointsAction()
{
	static size_t idx = pointsActions.size()-1;

	// cycle or switch action ?
	if( placeHolderPoints->isChecked() )
	{
		idx++;
		if( idx > pointsActions.size()-1 ) idx = 0;	
	}

	swapPointsAction(pointsActions[idx],false);
	firePointsAction();
}
//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cycleSegmentsAction()
{
	static size_t idx = segmentsActions.size()-1;

	// cycle or switch action ?
	if( placeHolderSegments->isChecked() )
	{
		idx++;
		if( idx > segmentsActions.size()-1 ) idx = 0;	
	}

	swapSegmentsAction(segmentsActions[idx],false);
	fireSegmentsAction();
}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cycleArcsAction()
{
	static size_t idx = arcsActions.size()-1;

	// cycle or switch action ?
	if( placeHolderArcs->isChecked() )
	{
		idx++;
		if( idx > arcsActions.size()-1 ) idx = 0;	
	}

	swapArcsAction(arcsActions[idx],false);
	fireArcsAction();
}

//-------------------------------------------------------------------------------------------------
//void StandAloneWindow::cycleTangentsAction()
//{
//	static size_t idx = 0;
//
//	// cycle or switch action ?
//	if( placeHolderMain->isChecked() )
//	{
//		idx++;
//		if( idx > mainActions.size()-1 ) idx = 0;	
//	}
//
//	swapMainAction(mainActions[idx],false);
//	fireMainAction();
//}

//-------------------------------------------------------------------------------------------------
//void StandAloneWindow::cycleWindspiralAction()
//{
//	static size_t idx = 0;
//
//	// cycle or switch action ?
//	if( placeHolderWindspiral->isChecked() )
//	{
//		idx++;
//		if( idx > mainActions.size()-1 ) idx = 0;	
//	}
//
//	swapMainAction(mainActions[idx],false);
//	fireMainAction();
//}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cycleLociAction()
{
	static size_t idx = lociActions.size()-1;

	// cycle or switch action ?
	if( placeHolderLoci->isChecked() )
	{
		idx++;
		if( idx > lociActions.size()-1 ) idx = 0;	
	}

	swapLociAction(lociActions[idx],false);
	fireLociAction();
}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cycleShapesAction()
{
	static size_t idx = shapesActions.size()-1;

	// cycle or switch action ?
	if( placeHolderShapes->isChecked() )
	{
		idx++;
		if( idx > shapesActions.size()-1 ) idx = 0;	
	}

	swapShapesAction(shapesActions[idx],false);
	fireShapesAction();
}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cycleCurveOperationsAction()
{
	static size_t idx = operationsActions.size()-1;

	// cycle or switch action ?
	if( placeHolderCurveOperations->isChecked() )
	{
		idx++;
		if( idx > operationsActions.size()-1 ) idx = 0;	
	}

	swapCurveOperationsAction(operationsActions[idx],false);
	fireCurveOperationsAction();
}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cycleShapeOperationsAction()
{
	static size_t idx = shapeOperationsActions.size()-1;

	// cycle or switch action ?
	if( placeHolderShapeOperations->isChecked() )
	{
		idx++;
		if( idx > shapeOperationsActions.size()-1 ) idx = 0;	
	}

	swapShapeOperationsAction(shapeOperationsActions[idx],false);
	fireShapeOperationsAction();
}

//-------------------------------------------------------------------------------------------------
void StandAloneWindow::cycleMeasuresAction()
{
	static size_t idx = measuresActions.size()-1;

	// cycle or switch action ?
	if( placeHolderMeasures->isChecked() )
	{
		idx++;
		if( idx > measuresActions.size()-1 ) idx = 0;	
	}

	swapMeasuresAction(measuresActions[idx],false);
	fireMeasuresAction();
}
